package com.ruoyi.cart.service.impl;

import com.ruoyi.cart.domain.PmsCartSku;
import com.ruoyi.cart.mapper.PmsCartSkuMapper;
import com.ruoyi.cart.service.IPmsCartSkuService;
import com.ruoyi.common.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 购物车商品明细Service业务层处理
 *
 * @author jorker
 * @date 2024-12-27
 */
@Service
public class PmsCartSkuServiceImpl implements IPmsCartSkuService
{
    @Autowired
    private PmsCartSkuMapper pmsCartSkuMapper;
    @Autowired
    private RedisService redisService;
//    @Autowired
//    private RedissonClient redisson;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询购物车商品明细
     *
     * @param id 购物车商品明细主键
     * @return 购物车商品明细
     */
    @Override
    public PmsCartSku selectPmsCartSkuById(Long id)
    {
        return pmsCartSkuMapper.selectPmsCartSkuById(id);
    }

    /**
     * 查询购物车商品明细列表
     *
     * @param pmsCartSku 购物车商品明细
     * @return 购物车商品明细
     */
    @Override
    public List<PmsCartSku> selectPmsCartSkuList(PmsCartSku pmsCartSku)
    {
        return pmsCartSkuMapper.selectPmsCartSkuList(pmsCartSku);
    }

    /**
     * 新增购物车商品明细
     *
     * @param pmsCartSku 购物车商品明细
     * @return 结果
     */
    @Override
    public int insertPmsCartSku(PmsCartSku pmsCartSku)
    {
        return pmsCartSkuMapper.insertPmsCartSku(pmsCartSku);
    }

    /**
     * 修改购物车商品明细
     *
     * @param pmsCartSku 购物车商品明细
     * @return 结果
     */
    @Override
    public int updatePmsCartSku(PmsCartSku pmsCartSku)
    {
        return pmsCartSkuMapper.updatePmsCartSku(pmsCartSku);
    }

    /**
     * 批量删除购物车商品明细
     *
     * @param ids 需要删除的购物车商品明细主键
     * @return 结果
     */
    @Override
    public int deletePmsCartSkuByIds(Long[] ids)
    {
        return pmsCartSkuMapper.deletePmsCartSkuByIds(ids);
    }

    /**
     * 删除购物车商品明细信息
     *
     * @param id 购物车商品明细主键
     * @return 结果
     */
    @Override
    public int deletePmsCartSkuById(Long id)
    {
        return pmsCartSkuMapper.deletePmsCartSkuById(id);
    }

    @Override
    public void deleteByCartId(Long cartId) {
        pmsCartSkuMapper.deleteByCartId(cartId);
    }

    @Override
    public void addBatch(List<PmsCartSku> pmsCartSkuList) {
        pmsCartSkuMapper.addBatch(pmsCartSkuList);
    }

//    @Override
//    public Boolean selectInventory(Long skuId) {
//
//        final String lockKey = "goods"+skuId;
//        Long longs = pmsCartSkuMapper.selectInventory(skuId);
//
//        String requestId = UUID.randomUUID().toString().replaceAll("-", "");
//        Boolean result = redisTemplate.opsForValue().setIfAbsent(lockKey, longs, 5, TimeUnit.MINUTES);
//        if (!result){
//            return Boolean.FALSE;
//        }
//
//        try {
//            //业务
//        }finally {
//            String script = "if (redis.call('get',KEYS[1]) == ARGV[1]) then"+
//                    "return redis.call('del',KEYS[1])"+
//                    "else"+
//                    "return 0"+
//                    "end";
//            DefaultRedisScript<Boolean> redisScript = new DefaultRedisScript<>();
//            redisScript.setResultType(Boolean.class);
//            redisScript.setScriptText(script);
//            redisTemplate.execute(redisScript, Collections.singletonList(lockKey),requestId);
//            MyDistributedLock myDistributedLock = new MyDistributedLock(redisTemplate);
//            if (myDistributedLock.tryLock(lockKey,requestId,5)){
//                try {
//                    Long result1 = (Long) redisTemplate.opsForValue().get(lockKey);
//                    if (result1 == 0){
//                        return Boolean.FALSE;
//                    }
//                    return Boolean.TRUE;
//                }finally {
//                    myDistributedLock.unLock(lockKey,requestId);
//                }
//            }
//        }
////        Object key = redisService.getCacheObject("goods:" + skuId);
////        if (key == null){
////            synchronized (this){
////                key = redisService.getCacheObject("goods:"+skuId);
////                if (key != null){
////                    return (Long) key;
////                }
////                Long expire = (long) (Math.random() * 100);
////                redisService.setCacheObject("goods:"+skuId,longs,expire, TimeUnit.MINUTES);
////            }
////        }
////        return longs;
//        return Boolean.FALSE;
//    }
//
//    public class MyDistributedLock{
//
//        private final RedisTemplate redisTemplate;
//        public MyDistributedLock(RedisTemplate redisTemplate){
//            this.redisTemplate = redisTemplate;
//        }
//
//        public Boolean tryLock(String lockKey, String requestId, int expireSeconds){
//            return redisTemplate.opsForValue().setIfAbsent(lockKey, requestId, expireSeconds, TimeUnit.SECONDS);
//        }
//
//        public Boolean unLock(String lockKey, String requestId){
//            String script = "if (redis.call('get',KEYS[1]) == ARGV[1]) then"+
//                    "return redis.call('del',KEYS[1])"+
//                    "else"+
//                    "return 0"+
//                    "end";
//            DefaultRedisScript<Boolean> redisScript = new DefaultRedisScript<>();
//            redisScript.setResultType(Boolean.class);
//            redisScript.setScriptText(script);
//            return (Boolean) redisTemplate.execute(redisScript, Collections.singletonList(lockKey),requestId);
//        }
//
//    }


}
